package com.example.partyplaner;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class TasksOrganizerActivity extends ListActivity implements
		AsyncResponse {

	ArrayList<EventTask> tasksToDo;
	ArrayList<TaskList> tasksListToPass;
	ArrayList<Integer> organizerTask;
	ArrayAdapter<EventTask> adapter;
	static String value;
	private String responseMsg;
	private int responseCode;
	String taskListID;
	DatabaseConnection db;
	int getTasks;
	int logout = 0;
	String filename = "Tasks";
	FileOutputStream outputStream;
	FileInputStream inputStream;
	int start = 0;
	int tasklists = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tasks);
		tasksListToPass = new ArrayList<TaskList>();
		organizerTask = new ArrayList<Integer>();
		tasksToDo = new ArrayList<EventTask>();
		taskListID = null;
		getTasks = 0;
		Bundle extras = getIntent().getExtras();
		if (extras != null) {

			TaskList list = (TaskList) extras.getSerializable("TASK_list1");
			db = new DatabaseConnection(TasksOrganizerActivity.this);
			db.delegate = TasksOrganizerActivity.this;
			
			db.execute("http://partyplanner.mais2014.cloudbees.net/rest/WebService/ShowTasks/"+ MainActivity.token, "GET",Integer.toString(list.ID));
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.tasks, menu);
		return true;
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Intent intent = new Intent(getApplicationContext(),SingleTaskActivity.class);
		EventTask taskToPass = (EventTask) this.getListAdapter().getItem(position);
		intent.putExtra("TASK_ID", taskToPass);
		startActivity(intent);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.logout) {
			this.db = new DatabaseConnection(TasksOrganizerActivity.this);
			this.db.delegate = TasksOrganizerActivity.this;
			db.execute("http://partyplanner.mais2014.cloudbees.net/rest/WebService/Logout","GET", MainActivity.token);
			logout = 1;
		}
		return super.onOptionsItemSelected(item);
	}

	public void processFinish(WebserviceResponse output) {
		if (output != null) {
			responseMsg = output.getResponseMsg();
			responseCode = output.getResponseCode();
			Log.i("PartyPlanner", "process Finish");
			if (responseCode == 200) {
				if (logout != 1) {

					try {
						JSONArray jArray = new JSONArray(responseMsg);
						if (!jArray.isNull(0)) {
							Log.i("PartyPlanner", "not NULL");
							EventTask task2;
							for (int it = 0; it < jArray.length(); it++) {
								Log.i("PartyPlanner", "Array not null");
								JSONObject jObject = jArray.getJSONObject(it);
								Log.i("PartyPlanner",jObject.getString("title"));
								Log.i("PartyPlanner",jObject.getString("description"));
								EventTask task = new EventTask(
										1,
										jObject.getString("title"), "cos",
										jObject.getString("description"),
										jObject.getInt("realized"));
								tasksToDo.add(task);
								Log.i("PartyPlanner", "new Task");
							}
						} else {
							Log.i("PartyPlanner", "else");
							EventTask task1 = new EventTask(0,"No tasks available", "", "", 0);
							tasksToDo.add(task1);
							adapter = new ArrayAdapter<EventTask>(this,android.R.layout.simple_list_item_1, tasksToDo);
							setListAdapter(adapter);
						}

					} catch (JSONException e) {
						e.printStackTrace();
					}
					adapter = new ArrayAdapter<EventTask>(this,android.R.layout.simple_list_item_1, tasksToDo);
					setListAdapter(adapter);

				} else {
					Intent intent = new Intent(getApplicationContext(),MainActivity.class);
					startActivity(intent);
				}
			} else {
				Log.i("PartyPlanner", "Fail");
			}
		} else {
			Log.i("PartyPlanner", "NULL");
		}
	}

	public Dialog onCreateDialog(int dialogId) {
		switch (dialogId) {
		case MainActivity.PLEASE_WAIT_DIALOG:
			ProgressDialog dialog = new ProgressDialog(TasksOrganizerActivity.this);
			dialog.setTitle("Connecting");
			dialog.setMessage("Please wait....");
			dialog.setCancelable(true);
			return dialog;
		default:
			break;
		}
		return null;
	}
}
